package com.lyh.controller;

import com.google.common.base.Objects;
import com.google.common.collect.Maps;
import com.lyh.dao.ILogDAO;
import com.lyh.dao.IUserDAO;
import com.lyh.model.Log;
import com.lyh.model.LogTypeEnum;
import com.lyh.model.User;
import com.lyh.model.UserStatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 由于功能都比较简单 故没有写service层
 *
 * @author hao.su
 */
@Api(description = "用户信息")
@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private IUserDAO mapper;

	@Autowired
	private ILogDAO loggerDao;

	private Logger logger = LoggerFactory.getLogger(UserController.class);

	@ApiOperation(value = "查询用户", notes = "查询用户", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "/user.json", params = "name", method = RequestMethod.GET)
	@ResponseBody
	public Object selectUser(
			@ApiParam(required = true, value = "用户名") @RequestParam(required = true) String name) {
		System.out.println("param name:" + name);
		User user = null;
		try {
			user = mapper.selectUser(name);
		} catch (Exception e) {
			logger.info("查询用户失败", e);
			e.printStackTrace();
		}
		Map<String, Object> map = Maps.newLinkedHashMap();
		map.put("status", 0);
		map.put("data", user);
		map.put("message", null);
		return map;
	}

	@ApiOperation(value = "添加用户", notes = "添加用户", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Object addUser(
			@RequestBody User user,
			HttpServletResponse response) {
		Map<String, Object> map = null;
		try {
			map = Maps.newLinkedHashMap();
			mapper.addUser(user);
			// 记录日志
			Log logger = new Log();
			logger.setUserId(user.getId());
			logger.setType(LogTypeEnum.create_user);
			logger.setContent("create user:" + user.getName());
			logger.setCreatetime(new Date());
			loggerDao.insertLog(logger);
			map.put("status", 0);
			map.put("data", user.getId());
			map.put("message", null);
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("添加用户失败", e);
		}

		return map;
	}

	@RequestMapping(value = "/update.json", params = "name", method = RequestMethod.POST)
	@ResponseBody
	public Object updateUser(@RequestBody User user, @RequestParam String name) {
		Map<String, Object> map = Maps.newLinkedHashMap();
		user.setName(name);
		User u1 = null;
		User oldUser = null;
		StringBuilder content = null;
		try {
			oldUser = mapper.selectUser(name);
			Date d1 = oldUser.getBirth();
			mapper.updateUser(user);
			u1 = mapper.selectUser(name);
			Date d2 = u1.getBirth();
			if (!(d1 == d2)) {
				Date birthDate = user.getBirth();
				content = new StringBuilder();
				content.append("[birth]"
						+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
						.format(oldUser.getBirth())
						+ "=>"
						+ new SimpleDateFormat("yyyy-MM-dd HH:ss")
						.format(birthDate));
			}

			if (!Objects.equal(user.getStatus(), u1.getStatus())) {
				UserStatusEnum statusEnum = user.getStatus();
				content.append(content + "[status]" + oldUser.getStatus() + "=>" + statusEnum);
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.info("更新用户失败", e);
		}
		Log logger = new Log();
		logger.setUserId(u1.getId());
		logger.setType(LogTypeEnum.update_user);
		logger.setContent(content + "");
		logger.setCreatetime(new Date());
		loggerDao.insertLog(logger);
		map.put("status", 0);
		map.put("data", u1);
		map.put("message", null);
		return map;
	}

}
